﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AirlineProject.Models;

namespace AirlineProject.Controllers
{
    public class TicketController : Controller
    {
        public ActionResult Message()
        {
            return View();
        }
        public ActionResult InfoBill(string id)
        {
            BillModels bm = new BillModels();
            ViewData["infobill"] = bm.InfoBill(id);
            return View();
        }
        public ActionResult InfoTicket(string id)
        {
            TicketModels tm = new TicketModels();
            ViewData["infoticket"] = tm.InfoTicket(id);
            return View();
        }
        public ActionResult InfoPrice(int id)
        {
            TypeTicketModels ttm = new TypeTicketModels();
            Response.Write(ttm.InfoTypeTicket(id).type_price);
            return null;
        }

        public ActionResult SetTicket(string id, string billid, int flight_id, string ticket_date, int type_id, int child_num, string class1, double ticket_price, int payment_id)
        {
            string v = string.Empty;
            if (Session["username"] == null)
            {
                v = "Login";
                ViewData["message"] = "You need to login the system.";
                return RedirectToAction("Login", "Account");
            }
            else
            {
                string bill_id = DateTime.Now.ToString("yyyyMMddHHmmss");
                BillModels bm = new BillModels();
                FlightModels fm = new FlightModels();
                ViewData["infoflight"] = fm.InfoFlight(flight_id);
                User ur = (User)Session["username"];
                bm.AddBill(bill_id,ur.user_id,DateTime.Now);
                ViewData["bill"] = bm.InfoBill(bill_id);
                TicketModels tm = new TicketModels();
                if(bm.AddBill(bill_id,ur.user_id,DateTime.Now) != null )
                {
                   
                    {
                        string ticket_id = DateTime.Now.ToString("yyyyMMddHHmmss");
                        Bill b = (Bill)ViewData["bill"];
                        Flight f = (Flight)ViewData["infoflight"];
                        List<TypeTicket> listtypeticket = (List<TypeTicket>)ViewData["listtypeticket"];
                        tm.AddTicket(ticket_id, b.bill_id, f.flight_id, DateTime.Now, type_id,  child_num, class1, ticket_price, 1, payment_id);
                        Session["user_name"] = ur;
                        v = "Message";
                    }
                }
            }
            return View(v);
        }
        
    }
}
